package com.example.huangds.myapplication;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

public class GoodsListAdapter extends ArrayAdapter<GoodsInfo> {
    private Activity activity;
    private int resourceId;

    public GoodsListAdapter(Context context, int textViewResourceId, List<GoodsInfo> objects, Activity activity) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
        this.activity = activity;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GoodsInfo goodsInfo = getItem(position);   //获取当前项的实例
        View view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);

        ImageView goodsImage = (ImageView) view.findViewById(R.id.goods_image);
        TextView goodsName = (TextView) view.findViewById(R.id.goods_name);
        TextView goodsDesc = (TextView) view.findViewById(R.id.goods_description);
        TextView goodsNowPrice = (TextView) view.findViewById(R.id.goods_nowprice);
        TextView goodsSell = (TextView) view.findViewById(R.id.goods_sell);
        TextView goodsStore = (TextView) view.findViewById(R.id.goods_store);

        goodsName.setText(goodsInfo.getGoodsNm());
        goodsDesc.setText(goodsInfo.getGoodsDesc());
        goodsNowPrice.setText(goodsInfo.getNewPrice());
        goodsSell.setText(goodsInfo.getGoodsSell());
        goodsStore.setText(goodsInfo.getGoodsStore());

        // Item点击事件
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TextView textView = view.findViewById(R.id.goods_name);
                String text = textView.getText().toString();

                Intent intent=new Intent();
                intent.setClass(activity,GoodsInfoActivity.class);
                activity.startActivity(intent);
            }
        });

        Glide.with(activity).load(goodsInfo.getImageUrl()).asBitmap().into(goodsImage);

        return view;
    }

}
